//------------------------------------------------------------------------------
// <copyright file="XmlExceptionReplaceHandler.cs" company="Damian Antonowicz">
//       copyright © 2010 Damian Antonowicz
// </copyright>
// <author>Damian Antonowicz</author>
// <email>poczta@damianantonowicz.pl</email>
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration;
using System.Collections.Specialized;
using System.Xml;
using System.Xml.Schema;

namespace DotBeer.Business.Components.Exceptions
{
    [ConfigurationElementType(typeof(CustomHandlerData))]
    public class XmlExceptionReplaceHandler : IExceptionHandler
    {
        #region constructors

        public XmlExceptionReplaceHandler(NameValueCollection ignore)
        {
        }

        #endregion

        #region public methods

        public Exception HandleException(Exception exception, Guid handlingInstanceId)
        {
            if (exception is XmlException ||
                exception is XmlSchemaValidationException)
            {
                return new ApplicationException(String.Format("Loading xml file contains corrupted data:\n\n{0}\n\nLoading has been cancelled.", exception.Message));
            }
            else
            {
                return new Exception("Loading xml file contains corrupted data. Loading has been cancelled.");
            }
        }

        #endregion
    }
}
